package fragment;

import java.util.List;
import java.util.Random;

import protocol.RecommendProtocol;

import base.BaseFragment;
import base.LoadingPager.LoadResult;
import utils.UIUtils;
import view.flyinout.ShakeListener;
import view.flyinout.ShakeListener.OnShakeListener;
import view.flyinout.StellarMap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class RecommendFragment extends BaseFragment {

	private List<String> mDatas;
	private ShakeListener mShakeListener;

	/**
	 * 
	 */
	@Override
	protected LoadResult initData() {
		SystemClock.sleep(2000);
		//成功
		//失败
		//错误
		
		RecommendProtocol protocol = new RecommendProtocol();
		
		try {
			mDatas = protocol.loadData(0);
			return checkState(mDatas);
		} catch (Exception e) {
			e.printStackTrace();
			return LoadResult.ERROR;
		}
		
	}

	@Override
	protected View initSuccessView() {
		final StellarMap stellarMap = new StellarMap(UIUtils.getContext());
		final RecommendAdaptet adapter = new RecommendAdaptet();
		//设置适配器
		stellarMap.setAdapter(adapter);
		//设置拆分规则
		stellarMap.setRegularity(15, 20);
		//设置首页选中
		stellarMap.setGroup(0, true);
		//加入摇一摇的功能
		mShakeListener = new ShakeListener(UIUtils.getContext());
		
		mShakeListener.setOnShakeListener(new OnShakeListener() {
			
			@Override
			public void onShake() {
				int currentGroup = stellarMap.getCurrentGroup();
				if (currentGroup == adapter.getGroupCount() -1) {
					currentGroup  =0;
				}else{
					currentGroup++;
				}
				//切换
				stellarMap.setGroup(currentGroup, true);
			}
		});
		return stellarMap;
	}
	
	@Override
	public void onPause() {
		if (mShakeListener != null) {
			mShakeListener.pause();
		}
		super.onPause();
	}
	
	@Override
	public void onResume() {
		if (mShakeListener != null) {
			mShakeListener.resume();
		}
		super.onResume();
	}
	
	

	//自定义一个适配器
	class RecommendAdaptet implements  StellarMap.Adapter{

		
		private static final int PAGESIZE = 15;
	

		@Override
		public int getGroupCount() {
			if (mDatas.size() % PAGESIZE !=0) {
				return mDatas.size()/ PAGESIZE +1;
			}
			
			return mDatas.size()/PAGESIZE;
		}

		@Override
		public int getCount(int group) {//每组由多小个	
			//最后一页的时候，要去处理这种情况
			if (group == getGroupCount()-1) {
				if (mDatas.size() % PAGESIZE !=0) {
					return mDatas.size()% PAGESIZE;
				}
			}
			return PAGESIZE;
		}

		@Override
		public View getView(int group, int position, View convertView) {//返回具体的View
			int location = group *PAGESIZE +position;
			
			TextView tv = new TextView(UIUtils.getContext());
			int padding = UIUtils.dip2px(4);
			tv.setPadding(padding, padding, padding, padding);
			tv.setText(mDatas.get(location));
			
			//设置随机大小
			Random random =new Random();
			tv.setTextSize(random.nextInt(4)+12);
			
			int alpha = 255;
			int red = random.nextInt(190)+30;
			int green = random.nextInt(190)+30;
			int blue = random.nextInt(190)+30;
			int color = Color.argb(alpha, red, green, blue);
			
			tv.setTextColor(color);
			return tv;
		}

		@Override
		public int getNextGroupOnPan(int group, float degree) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public int getNextGroupOnZoom(int group, boolean isZoomIn) {
			// TODO Auto-generated method stub
			return 0;
		}
		
	}
}
